package com.edu.koTA07.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.edu.koTA07.bean.BarangBean;
import com.edu.koTA07.bean.PesananDetailBean;
import com.edu.koTA07.takingOrder.R;
import com.edu.koTA07.utils.BarangUtils;

public class PesananListAdapter extends BaseAdapter implements OnClickListener{

	ArrayList<PesananDetailBean> pesananDetails;
	List<BarangBean> listBarangCurrent;
	BarangUtils util;
	Context context;
	// private ImageLoader imageLoader;
	public PesananListAdapter(Context context,
			ArrayList<PesananDetailBean> pesananDetails, List<BarangBean> listBarangCurrent) {
		// TODO Auto-generated constructor stub
		super();
		inflater = (LayoutInflater) context
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.pesananDetails = pesananDetails;
		this.listBarangCurrent = listBarangCurrent;
		this.context = context;
	}
	private static LayoutInflater inflater = null;

	public int getCount() {
		return pesananDetails.size();
	}

	public PesananDetailBean getItem(int position) {
		return (null == pesananDetails) ? null : pesananDetails.get(position);
	}
	
	public ArrayList<PesananDetailBean> getItemAll() {
		return (null == pesananDetails) ? null : pesananDetails;
	}

	public long getItemId(int position) {
		return position;
	}
	
	class ViewHolder {
		TextView textLine;
		TextView price;
		EditText kuantitasBarang;
		TextView subtotal;
		Button btnRemove;
		TextView potongan;
	     //ImageView iconLine;
	     //Button buttonLine;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		View vi = convertView;
		util = new BarangUtils(context);
		
		if (convertView == null) {
			vi = inflater.inflate(R.layout.content_pesanan, null);
			holder = new ViewHolder();
			holder.textLine = (TextView) vi.findViewById(R.id.isi_produk);
			holder.price = (TextView) vi.findViewById(R.id.isi_price);
			holder.kuantitasBarang = (EditText) vi.findViewById(R.id.isi_kuantitas);
			holder.subtotal = (TextView) vi.findViewById(R.id.isi_subtotal);
			holder.btnRemove = (Button) vi.findViewById(R.id.button_remove);
			vi.setTag(holder);
		}
		else 
			holder = (ViewHolder) vi.getTag();
		
		
		BarangBean barangbean = util.getBarangById(pesananDetails.get(position).getIDBarang());
		String barang = barangbean.getNamaBarang();
		//holder.kuantitasBarang.selectAll();
		String ckuantitas = holder.kuantitasBarang.getText().toString();
		int kuantitas = Integer.parseInt(ckuantitas);
		if(ckuantitas.equals("")){
			kuantitas = 1;
		}
		long harga = barangbean.getHargaBarang();
		String charga = "" + harga;
		
		
		
		long subtotal = kuantitas * harga;
		
		float total = subtotal - 0;//ada potongan
		String csubtotal = ""+total;
		holder.textLine.setText(barang);
		holder.price.setText(charga);
		
		holder.subtotal.setText(csubtotal);
		
		holder.kuantitasBarang.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				notifyDataSetChanged();
				
			}
		});
 
		if(ckuantitas.equals("")){
			holder.kuantitasBarang.setText("");
		}else {
			holder.kuantitasBarang.setText(ckuantitas);
		}
		pesananDetails.get(position).setKuantitasPesanan(kuantitas);
		pesananDetails.get(position).setPotongan(total);
		pesananDetails.get(position).setTotal(total);
		
		
		
		//
		//holder.kuantitasBarang.setFocusableInTouchMode(false);
		//holder.kuantitasBarang.setFocusable(false);
	    //holder.kuantitasBarang.setOnClickListener(this);
	    //holder.kuantitasBarang.setTag(pesananDetails.get(position));
		// Set the onClick Listener on this button
        
        holder.btnRemove.setFocusableInTouchMode(false);
        holder.btnRemove.setFocusable(false);
        holder.btnRemove.setOnClickListener(this);
        holder.btnRemove.setTag(pesananDetails.get(position));
        
        return vi;
	}

	public void onClick(View view) {
    	PesananDetailBean entry = (PesananDetailBean) view.getTag();
    	for(int i=0;i<listBarangCurrent.size();i++){
    		if(entry.getIDBarang().equals(listBarangCurrent.get(i).getIDBarang())) {
    			listBarangCurrent.get(i).setSelectedBarang(false);
    		}
    	}
    	pesananDetails.remove(entry);
    	
    	//pesananDetails.remove(view.getId());
        notifyDataSetChanged();

    }

	public List<BarangBean> getCurrentDataBack(){
		return listBarangCurrent;
	}
	
	public void forceReload() {
		notifyDataSetChanged();
	}	
}
